package com.situ.mall.job;

import com.situ.mall.pojo.Product;
import com.situ.mall.util.QiniuUtils;
import com.situ.mall.util.RedisConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Set;

@Configuration
public class DeleteImageJob {
    @Autowired
    private RedisTemplate redisTemplate;

    //
    @Scheduled(cron = "0 */2 * * * ?")
    public void deleteImage() {
        System.out.println("DeleteImageJob.deleteImage");
        //根据Redis中保存的两个set集合进行差值计算，获得垃圾图片名称集合
        Set<String> set =redisTemplate.opsForSet().difference(RedisConstant.UPLOAD_IMAGE, RedisConstant.UPLOAD_IMAGE_TO_DB);
        if (set != null) {
            for (String imageName : set) {
                //删除七牛云服务器上的图片
                QiniuUtils.deleteFileFromQiniu(imageName);
                System.out.println("删除图片: " + imageName);
                redisTemplate.opsForSet().remove(RedisConstant.UPLOAD_IMAGE, imageName);
            }
        }
    }


    @Bean
    public Product createProduct() {
        return  new Product();
    }
}
